
package com.idealighter.game.games.sirenniuniu.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.core.constant.room.RoomActiveConstant;
import com.idealighter.game.dictionary.dic.GamesDic;
import com.idealighter.game.dictionary.dic.SirenniuniuRoomDic;
import com.idealighter.game.dictionary.domain.GamesDomain;
import com.idealighter.game.dictionary.domain.SirenniuniuRoomDomain;
import com.idealighter.game.games.common.InstanceState;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuBaseRoom;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuRoom;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuSeat;
import com.idealighter.game.games.sirenniuniu.struct.SiRenNiuNiuTable;
import com.idealighter.utils.check.EmptyUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 四人牛牛数据管理.
 * 
 * @date 2016年1月20日 下午4:47:14
 *
 */
@Singleton
public class SiRenNiuNiuDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(SiRenNiuNiuDataMgr.class);

  private final Map<Integer, SiRenNiuNiuBaseRoom> roomMap = new HashMap<>();

  private SirenniuniuRoomDic roomDic;

  private final GamesDic gamesDic;

  // 玩家的座位
  private final Map<Long, SiRenNiuNiuSeat> playerSeats = new HashMap<>();


  /**
   * 构造函数.
   * 
   * @param roomDic .
   * @param gamesDic .
   */
  @Inject
  public SiRenNiuNiuDataMgr(SirenniuniuRoomDic roomDic, GamesDic gamesDic) {
    this.roomDic = roomDic;
    this.gamesDic = gamesDic;
    createRooms();
  }

  private void createRoom(SirenniuniuRoomDomain roomDom) {
    if (roomDom != null && roomDom.getIsActive() == RoomActiveConstant.ACTIVE) {
      GamesDomain gamesDom = gamesDic.get(Game.SIREN_NIUNIU.getType());
      if (gamesDom != null && gamesDom.getActive()) {
        roomMap.put(roomDom.getId(), new SiRenNiuNiuBaseRoom(roomDom));
        LOG.info("[四人牛牛]创建房间[{}][{}]", roomDom.getId(), roomDom.getName());
      }
    }
  }

  /**
   * 创建房间 .
   * 
   * @return
   */
  private void createRooms() {
    List<SirenniuniuRoomDomain> roomDomains = roomDic.list();
    if (EmptyUtil.listIsNotEmpty(roomDomains)) {
      for (SirenniuniuRoomDomain roomDom : roomDomains) {

        createRoom(roomDom);

      }
    }
  }

  /**
   * 重新加载房间，并返回最新房间id列表.
   *
   * @author abin
   * @date 2018年5月16日 下午2:48:38
   * @return 房间id列表.
   */
  public List<Integer> reloadRoom() {
    roomDic.load();
    List<Integer> ids = new ArrayList<>();
    GamesDomain gamesDom = gamesDic.get(Game.SIREN_NIUNIU.getType());
    if (gamesDom != null && gamesDom.getActive()) {
      List<SirenniuniuRoomDomain> rooms = roomDic.list();
      if (EmptyUtil.listIsNotEmpty(rooms)) {
        for (SirenniuniuRoomDomain roomDomain : rooms) {
          if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {
            ids.add(roomDomain.getId());
          }
        }
      }
    }
    return ids;
  }

  /**
   * 开启房间.
   *
   * @author abin
   * @date 2018年4月28日 下午2:14:26
   * @param roomId 房间id.
   * @return return old room(delete) list.
   */
  public List<SiRenNiuNiuRoom> startRoom(int roomId, boolean reload) {
    if (reload) {
      roomDic.load();
    }
    List<SiRenNiuNiuRoom> deleteRooms = new ArrayList<>();
    GamesDomain gamesDom = gamesDic.get(Game.SIREN_NIUNIU.getType());
    if (gamesDom != null && gamesDom.getActive()) {
      SirenniuniuRoomDomain roomDomain = roomDic.get(roomId);
      if (roomDomain != null && roomDomain.getIsActive() == RoomActiveConstant.ACTIVE) {

        SiRenNiuNiuBaseRoom baseRoom = roomMap.get(roomId);

        if (baseRoom == null) {
          createRoom(roomDomain);
        } else {
          deleteRooms = baseRoom.renewRoom(roomDomain);
        }
      }
    }
    return deleteRooms;
  }

  /**
   * 获取最新房间实例.
   * 
   * @param roomId 房间id.
   * @return 房间.
   */
  public SiRenNiuNiuRoom getNewestRoom(int roomId) {
    SiRenNiuNiuRoom room = null;
    SiRenNiuNiuBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      room = baseRoom.getNewInstance();
    }
    return room;
  }


  /**
   * 获取所有实例 .
   *
   * @author abin
   * @date 2018年4月28日 下午4:32:06
   * @param roomId 房间id.
   * @return 获取所有实例.
   */
  public List<SiRenNiuNiuRoom> deleteRoom(int roomId) {
    roomDic.load();
    List<SiRenNiuNiuRoom> rooms = null;
    SiRenNiuNiuBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      rooms = new ArrayList<>(baseRoom.deleteAllInstance());
    }
    return rooms;
  }

  /**
   * 实例id获取房间实例 .
   *
   * @author abin
   * @date 2018年4月27日 下午6:01:41
   * @param instanceId 实例id.
   * @return 21点房间.
   */
  public SiRenNiuNiuRoom getRoomByIntanceId(int instanceId) {
    SiRenNiuNiuRoom room = null;
    int roomId = getRoomIdByIntanceId(instanceId);
    SiRenNiuNiuBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      room = baseRoom.getByInstanceId(instanceId);
    }
    return room;
  }


  /**
   * 校验是否remove room.
   *
   * @author abin
   * @date 2018年4月28日 下午7:39:33
   * @param baseRoom 基本房间.
   */
  public void checkRemoveRoom(SiRenNiuNiuBaseRoom baseRoom) {
    if (baseRoom.allInstance() == null || baseRoom.allInstance().isEmpty()) {
      roomMap.remove(baseRoom.getRoomId());
      baseRoom = null;
    }

  }

  /**
   * 清空房间实例.
   *
   * @author abin
   * @date 2018年4月28日 下午5:55:32
   * @param roomId 房间id.
   * @param instanceId 实例id.
   */
  public boolean checkRemoveInstance(int roomId, int instanceId) {
    boolean remove = false;
    SiRenNiuNiuBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      SiRenNiuNiuRoom room = baseRoom.getByInstanceId(instanceId);

      Collection<SiRenNiuNiuTable> tables = room.tables();

      if (tables == null || tables.isEmpty()) {
        baseRoom.removeInstance(instanceId);
        checkRemoveRoom(baseRoom);
        remove = true;
      }
    }
    return remove;
  }

  /**
   * 获取所有最新的房间.
   * 
   * @return 21点房间列表.
   */
  public Collection<SiRenNiuNiuRoom> newestRooms() {
    List<SiRenNiuNiuRoom> tempRooms = new ArrayList<>();
    for (SiRenNiuNiuBaseRoom baseRoom : roomMap.values()) {
      SiRenNiuNiuRoom siRenNiuNiuRoom = baseRoom.getNewInstance();
      if (siRenNiuNiuRoom != null
          && siRenNiuNiuRoom.getInstanceState().equals(InstanceState.NORMAL)) {
        tempRooms.add(siRenNiuNiuRoom);
      }
    }
    return tempRooms;
  }

  /**
   * 获取所有的房间列表 .
   *
   * @author abin
   * @date 2018年4月27日 下午4:22:40
   * @return 所有的房间列表.
   */
  public Collection<SiRenNiuNiuRoom> allRooms() {
    List<SiRenNiuNiuRoom> tempRooms = new ArrayList<>();
    for (SiRenNiuNiuBaseRoom baseRoom : roomMap.values()) {
      Collection<SiRenNiuNiuRoom> allInstances = baseRoom.allInstance();
      if (allInstances != null && !allInstances.isEmpty()) {
        tempRooms.addAll(allInstances);
      }
    }
    return tempRooms;
  }

  public Collection<SiRenNiuNiuBaseRoom> allBaseRoom() {
    return roomMap.values();
  }



  /**
   * 获取牌桌.
   * 
   * @param tableId 桌子id.
   * @return 桌子.
   */
  public SiRenNiuNiuTable getTable(int tableId) {
    SiRenNiuNiuTable table = null;
    int roomId = getRoomIdByTableId(tableId);
    SiRenNiuNiuBaseRoom baseRoom = roomMap.get(roomId);
    if (baseRoom != null) {
      int instanceId = getInstanceIdByTableId(tableId);
      SiRenNiuNiuRoom room = baseRoom.getByInstanceId(instanceId);
      if (room != null) {
        table = room.getTables().get(tableId);
      }
    }
    return table;
  }

  /**
   * 根据桌子id计算房间实例id.
   * 
   * @param tableId 桌子Id.
   * @return 房间Id.
   */
  public int getInstanceIdByTableId(int tableId) {
    return tableId / SiRenNiuNiuTable.MAX_TABLE;
  }

  /**
   * 根据实例id计算房间id .
   *
   * @author abin
   * @date 2018年4月27日 下午5:54:05
   * @param instanceId 实例id.
   * @return 房间id.
   */
  public int getRoomIdByIntanceId(int instanceId) {
    return instanceId / SiRenNiuNiuRoom.MAX_INSTANCE;
  }

  /**
   * 根据桌子获取房间id .
   *
   * @author abin
   * @date 2018年4月27日 下午5:56:03
   * @param tableId 桌子id.
   * @return 房间id.
   */
  public int getRoomIdByTableId(int tableId) {
    return getRoomIdByIntanceId(getInstanceIdByTableId(tableId));
  }

  /**
   * 获取座位 .
   * 
   * @param playerId 玩家id.
   * @return
   */
  public SiRenNiuNiuSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }


  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, SiRenNiuNiuSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家seat .
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }



}
